import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

import departmentSAsyncRoutes from '@/router/modules/departments'
import employeesSAsyncRoutes from '@/router/modules/employees'
import settingsSAsyncRoutes from '@/router/modules/settings'
import permissionsSAsyncRoutes from '@/router/modules/permission'
import approvalsSAsyncRoutes from '@/router/modules/approvals'
import attendancesSAsyncRoutes from '@/router/modules/attendances'
import salarysSAsyncRoutes from '@/router/modules/salarys'
import socialSecuritysSAsyncRoutes from '@/router/modules/social_securitys'

// 创建动态路由
export const asyncRoutes = [
  // 放动态路由对象
  departmentSAsyncRoutes,
  settingsSAsyncRoutes,
  employeesSAsyncRoutes,
  permissionsSAsyncRoutes,
  approvalsSAsyncRoutes,
  attendancesSAsyncRoutes,
  salarysSAsyncRoutes,
  socialSecuritysSAsyncRoutes
]

// 静态路由
export const constantRoutes = [
  {
    // excel上传路由
    path: '/import',
    component: () => import('@/views/excel/upload-excel.vue'),
    hidden: true
  },
  {
    path: '/login', // 路由懒加载中的魔法注释 通过在注释中指定webpackChunkName，可以人为设置这个文件的名字
    component: () => import(/* webpackChunkName:"login" */'@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },
  {
    path: '/demo',
    component: () => import('@/views/demo/index.vue')
  }
  // 如果404不写在动态路由中 当登录完成后匹配动态路由时 会被*匹配掉 走不到动态路由 所以会去404页面
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 最终路由: 由静态路由和动态路由合并而成
  routes: [...constantRoutes] // 数组合并[...arr,...arr1]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由的方法
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
